package com.android.djlmapp.activity.my;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.android.djlmapp.R;
import com.android.djlmapp.activity.login.LoginActivity;
import com.android.djlmapp.activity.my.set.ChangeAccountActivity;
import com.android.djlmapp.activity.my.set.PermissionActivity;
import com.android.djlmapp.adapter.msg.MsgListActivity;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.model.my.UpdateInfo;
import com.android.djlmapp.model.my.UpdateUrlInfo;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.djlmapp.tools.global.UserInfoUtils;
import com.android.djlmapp.tools.msg.ExampleUtil;
import com.android.djlmapp.tools.my.CacheDataMangerUtils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.GetRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


public class MySettingActivity extends BaseActivity {
    public static final String TAG = "djlm-my: ";
    @BindView(R.id.my_new_img)
    ImageView myNewImg;
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.my_setting_notes)
    RelativeLayout mySettingNotes;
    @BindView(R.id.my_setting_permission)
    RelativeLayout mySettingPermission;
    //缓存清理
    @BindView(R.id.my_clear_val)
    TextView myClearVal;
    @BindView(R.id.my_setting_clear)
    RelativeLayout mySettingClear;
    @BindView(R.id.my_new_val)
    TextView myNewVal;
    @BindView(R.id.my_setting_new)
    RelativeLayout mySettingNew;

    private Unbinder mUnbinder;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_setting);
        mUnbinder = ButterKnife.bind(this);

        mContext = getApplicationContext();
        initData();
        initView();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        homeIvReadpoint.setVisibility(View.GONE);
        topNotes.setVisibility(View.GONE);
        tvName.setText("设置");
        myNewVal.setText(ExampleUtil.GetVersion(mContext));


        try {
            myClearVal.setText(CacheDataMangerUtils.getTotalCacheSize(this));
        } catch (Exception e) {
        }

        getVersion(GlobalUrls.UPDATE_URL);
    }

    @OnClick({R.id.my_setting_notes, R.id.my_setting_permission, R.id.my_setting_clear,
            R.id.my_setting_new, R.id.ll_top_back, R.id.btn_exit, R.id.my_setting_CA})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_setting_notes:
                startActivity(new Intent(getApplicationContext(), MsgListActivity.class));
                break;
            case R.id.my_setting_permission:
                startActivity(new Intent(getApplicationContext(), PermissionActivity.class));
                break;
            case R.id.my_setting_clear:
                CacheDataMangerUtils.clearAllCache(this);
                try {
                    if (CacheDataMangerUtils.getTotalCacheSize(MySettingActivity.this).startsWith("0")) {
                        myClearVal.setText("0KB");
                        showToast("清理缓存成功");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.my_setting_new:
                Log.i(TAG, "jiance");
                update();
                break;
            case R.id.ll_top_back:
                finish();
                break;
            case R.id.btn_exit:
                exit();
                break;
            case R.id.my_setting_CA:
                startActivity(new Intent(getApplicationContext(), ChangeAccountActivity.class));
                break;

        }
    }

    public void getVersion(String url) {
        GetRequest<String> post = OkGo.get(url);
        post.execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Gson gson = new Gson();
                UpdateUrlInfo info = gson.fromJson(response.body(), UpdateUrlInfo.class);
                String version = info.getVersion();
                Log.i(TAG, version);

//                //获取系统版本号
//                String versionLoc = ExampleUtil.GetVersion(mContext);
//                Log.i(TAG, "versionLoc: " + versionLoc);

                if (new UpdateInfo(mContext).conpileVersion(version)) {
                    myNewImg.setVisibility(View.VISIBLE);
                    myNewVal.setText("有新版本可更新");
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                Log.i(TAG, "" + response.body());

            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
    }

    private void exit() {
        //请求退出
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("是否退出登录");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 退出登录
                exitLogin();
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();

        //跳转登录页

    }

    public void exitLogin() {

        MyApplication.getInstance().getHttpTools().exit(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "exit return : " + response.body());
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                ToastUtils.showShortToast(mContext, "请检查您的网络..");

            }

            @Override
            public void onFinish() {
                super.onFinish();
                UserInfoUtils utils = UserInfoUtils.getUserInfoUtilsInstance();
                utils.setToken("");
                utils.setIsLog(false);

                startActivity(new Intent(MySettingActivity.this, LoginActivity.class)
                        .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
                                | Intent.FLAG_ACTIVITY_NEW_TASK));

                finish();
            }
        });
    }

    private void update() {
        UpdateInfo info = new UpdateInfo(mContext);
        info.update();

        Log.i(TAG, "更新检测: " + info.getIsUpdate());

        if(UpdateInfo.getIsUpdate()){
            ToastUtils.showLongToast(this, "已经是最新了~");
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
